using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Xeneize.SolaraBL;

namespace WIntegrator.Pages
{
    public partial class ManageDataViews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                //Set Select Parameter of the grid's data object
                this.ObjectDataSource_DataViews.SelectParameters.Clear();
                this.ObjectDataSource_DataViews.SelectParameters.Add("UserId", Membership.GetUser().ProviderUserKey.ToString());
                this.ObjectDataSource_DataViews.SelectMethod = "GetAll";
                this.ObjectDataSource_DataViews.DeleteMethod = "Delete";
                this.ObjectDataSource_DataViews.TypeName = "Xeneize.SolaraBL.SolaraDataViews";                
            }
        }

        /// <summary>
        /// I need to redo this function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Save_Click(object sender, EventArgs e)
        {
            SolaraDataView view = new SolaraDataView();
            try
            {
                if (this.txtName.Text.Length != 0) // We know they are creating a data view
                {
                    if (this.ddStatus.SelectedIndex != 0)  // Is a Status data view
                    {   
                        //view.Description = this.txtName.Text;
                        //view.DataViewType = (SolaraDataViews.DVType)Convert.ToInt32(this.ddCompanyDepartments.SelectedValue);
                        //view.Save(, , Convert.ToInt32(this.ddStatus.SelectedValue), Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString(), this.isRedFlag.Checked);
                    }
                    else if (this.ddProductionLocation.SelectedIndex != 0 && this.ddCompanyDepartments.SelectedIndex != 0)
                    {
                        //view.Create(this.txtName.Text, (SolaraDataViews.DVType)Convert.ToInt32(this.ddCompanyDepartments.SelectedValue), Convert.ToInt32(this.ddStatus.SelectedValue), Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString(), this.isRedFlag.Checked);
                    }
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }
            finally
            {
                this.txtName.Text = string.Empty;
                this.ddProductionLocation.SelectedIndex = 0;
                view = null;
                this.RadGrid1.DataBind();
            }
        }

        protected void ddProductionLocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Look for data only if the production is valid (not Select One)
            if (this.ddProductionLocation.SelectedIndex != 0)
            {
                this.ddCompanyDepartments.Items.Clear();
                //Reset Departments combo
                this.SqlDataSource_CompanyDepartments.SelectCommand = "SELECT [CompanyDepartmentID], [ProductionLocationID], [DepartmentName] FROM [CompanyDepartments] WHERE ([ProductionLocationID] = @ProductionLocationID) Order by DepartmentName";
                this.SqlDataSource_CompanyDepartments.SelectCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource_CompanyDepartments.SelectParameters.Clear();
                this.SqlDataSource_CompanyDepartments.SelectParameters.Add("ProductionLocationID", this.ddProductionLocation.SelectedValue);
                this.SqlDataSource_CompanyDepartments.DataBind();
            }
            else
            {
                // Remove all Items other than the first one (Select One)
                this.ddCompanyDepartments.Items.Clear();
                this.ddCompanyDepartments.Items.Add(new Telerik.Web.UI.RadComboBoxItem("Select one", "-1"));
            }
        }

    }
}
